package com.xyc.manager.permission.configuration;


import com.xyc.manager.permission.realm.MyRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.LinkedHashMap;
import java.util.Random;

/**
 * @Configuration 表示当前类替换xml配置文件
 *
 * 如果第三方jar包中的类需要spring管理，有两种方式：
 * 1、spring.xml中<bean id="" class=""></bean>
 * 2、注解替代<bean></bean>
 *
 *      AppConfigurationl类上加了@Configuration之后就等于是Spring.xml
 */
@Configuration
public class AppConfiguration {

    /**
     * @Bean注解等于<bean></bean>
     * @return
     */
    @Bean
    public Random provideRandom(){
        return new Random();
    }

    /*    <bean id="myRealm" class="com.qianfeng.shiro.realm.MyRealm">
    </bean>*/
    @Bean
    public MyRealm provideRealm(){
        return new MyRealm();
    }

    /*
     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm"/>
    </bean>
     */

    @Bean
    public DefaultWebSecurityManager provideSecurityManager(MyRealm myRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(myRealm);
        return defaultWebSecurityManager;
    }

    /*
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <!--没有登录的时候，拦截到统一登录页面-->
        <property name="loginUrl" value="/login.html"/>
        <!--没有权限的时候会统一跳转到此页面-->
        <property name="unauthorizedUrl" value="/nopermission.html"/>

        <!--登录之后才能使用-->
        <property name="filterChainDefinitions">
            <!--authc表示需要登录才能访问-->
            <value>
                /login.html=anon
                /login2=anon
                /**=authc
            </value>
        </property>
    </bean>
     */
    @Bean
    public ShiroFilterFactoryBean provideShiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/page/login.html");
        //此处需要使用LinkedHashMap保证顺序
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
        /*linkedHashMap.put("/page/login.html","anon");
        linkedHashMap.put("/user/login","anon");
        linkedHashMap.put("/js/**","anon");
        linkedHashMap.put("/css/**","anon");
        linkedHashMap.put("/img/**","anon");
        linkedHashMap.put("/fonts/**","anon");
        linkedHashMap.put("/dist/**","anon");
        linkedHashMap.put("/**","authc");*/
        shiroFilterFactoryBean.setFilterChainDefinitionMap(linkedHashMap);
        return shiroFilterFactoryBean;
    }

    /*<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean>*/

    /**
     * 只有添加如下两段配置，注解才能生效
     * @return
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator provideDefaultAdvisorAutoProxyCreator(){
        return new DefaultAdvisorAutoProxyCreator();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor provideAuthorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor sourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        sourceAdvisor.setSecurityManager(securityManager);
        return sourceAdvisor;
    }

    @Bean
    @LoadBalanced
    public RestTemplate provodeRestTemplate(){
        return new RestTemplate();
    }
}
